package com.znz.domain;

import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.tostring.RooToString;

import javax.persistence.*;
import java.math.BigDecimal;
import java.util.List;

/**
 *医院合约项目表
 * Created by IntelliJ IDEA.
 * User: johnson
 * Date: 5/9/15
 * Time: 7:34 下午
 */
@RooJavaBean
@RooToString
@RooJpaActiveRecord(versionField = "", identifierColumn = "id", identifierField = "id", table = "t_hospital_project_agreement")
public class ThospitalProjectAgreement {

	//	项目名称
	@Column(name = "project_name")
	private String projectName;
	//	项目id
    @OneToOne
    @JoinColumn(name="project_id")
	private Project projectID;
	//	项目价格
	@Column(name = "indicative_price")
	private BigDecimal indicativePrice;
    @Column(name = "commission")
    private String commission;

    //医院id
    @ManyToOne
    @JoinColumn(name = "hospital_id")
    private Thospital hospitalId;

    @Column(name = "status")
    private String status;


    public static void removeProjectByHospitalId(Long hospitalId)throws Exception{
        StringBuilder str_query =  new StringBuilder("SELECT o FROM ThospitalProjectAgreement o where  o.hospitalId=? ");
        TypedQuery<ThospitalProjectAgreement> q=entityManager().createQuery(str_query.toString(),ThospitalProjectAgreement.class);
        q.setParameter(1,hospitalId);
        List<ThospitalProjectAgreement> thospitalProjectAgreementList=q.getResultList();
        if(null!=thospitalProjectAgreementList && thospitalProjectAgreementList.size()>0){
            for(ThospitalProjectAgreement t :thospitalProjectAgreementList){
                t.remove();
            }
        }
    }

    public static List<ThospitalProjectAgreement> getThospitalProjectAgreementByWhere(String where,List<Object> params)throws Exception{
        StringBuilder strSql=new StringBuilder();
        strSql.append("SELECT o FROM ThospitalProjectAgreement o WHERE o.status!=-1 ");
        strSql.append(where);
        if (null!=params && params.size()>0){
            TypedQuery<ThospitalProjectAgreement> query=entityManager().createQuery(strSql.toString(),ThospitalProjectAgreement.class);
            int i=1;
            for (Object key :params){
                query.setParameter(i,key);
                i++;
            }
            return query.getResultList();
        }
        return  null;
    }

    public static  List<ThospitalProjectAgreement> getThospitalProject(Long hId){
        String sql="select * from t_hospital_project_agreement where hospital_id=? and status=0 ";
        Query query=entityManager().createNativeQuery(sql,ThospitalProjectAgreement.class);
        query.setParameter(1,hId);
        return  query.getResultList();
    }
}
